package com.example.iotmanagerment26.controller;

import com.example.iotmanagerment26.domain.JsonData;
import com.example.iotmanagerment26.domain.User;
import com.example.iotmanagerment26.domain.vo.UserCard;
import com.example.iotmanagerment26.domain.vo.UserOprecordVO;
import com.example.iotmanagerment26.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.ibatis.annotations.Options;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/v1/user")
@Tag(name="用户管理",description = "用户管理的接口")
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping("/selectUserByName")
    @Operation(summary = "通过名字查询用户信息")
    public JsonData selectUserByName(String userName){
        User result= userService.selectUserByName(userName);
        if (result!=null) {
            return new JsonData(1,result,"查找成功！");
        }else {
            return new JsonData(0,null,"查找失败！！！！");
        }
    }

    @GetMapping("/selectAllUser")
    @Operation(summary = "查询所有用户")
    public JsonData selectAllUser(){
        List<User> results= userService.selectAllUser();
        if (results!=null) {
            return new JsonData(1,results,"查找成功！");
        }else {
            return new JsonData(0,null,"查找失败！！！！");
        }
    }

    @PostMapping("/insertUser")
    @Operation(summary = "创建新的用户")
    public JsonData insertUser(@RequestBody User user){
        int result=userService.insertUser(user);
        if (result>=1){
            return new JsonData(1,user,"插入成功");
        }else {
            return new JsonData(0,result,"插入失败");
        }
    }

    @DeleteMapping("/deleteUserById")
    @Operation(summary = "通过用户id删除用户")
    public JsonData deleteUserById(int id){
        int result=userService.deleteUserById(id);
        if (result>=1){
            return new JsonData(1,id,"删除成功");
        }else {
            return new JsonData(0,result,"删除失败");
        }
    }

    @PostMapping("/updatePasswordByName")
    @Operation(summary = "修改用户密码")
    public JsonData updatePasswordByName(String name,String password){
        int result=userService.updatePasswordByName(name,password);
        if (result >=1){
            return new JsonData(1,name,"修改成功！");
        }else {
            return new JsonData(0,name,"修改失败!");
        }
    }

    @PostMapping("/login")
    @Operation(summary = "用户登录")
    public JsonData login(@RequestBody User user){
        User result = userService.login(user);
        if (result!=null){
            return new JsonData(1,result,"登录成功");
        }else {
            return  new JsonData(0,result,"登录失败");
        }
    }

    @GetMapping(value="selectUserCardById")
    @Operation(summary = "通过id查询用户会员信息")
    public JsonData selectUserWithCardById(int id){
        UserCard usercard=userService.selectUserWithCardById(id);
        if(usercard!=null){
            return new JsonData(0, usercard, "查询成功");
        }else {
            return new JsonData(0, usercard,"用户不存在");
        }
    }

    @GetMapping(value="selectAllUserWithCard")
    @Operation(summary = "查询所有用户信息（包括会员详细信息）")
    public JsonData selectAllUserWithCard(){
        List<UserCard> usercards=userService.selectAllUserWithCard();
        if(usercards!=null){
            return new JsonData(0, usercards, "查询成功");
        }else {
            return new JsonData(0, usercards,"用户不存在");
        }
    }

    @GetMapping(value="selectUserOprecordsById")
    @Operation(summary = "通过id查询用户绑定的设备")
    public JsonData selectUserOprecordsById(int id){
        UserOprecordVO records=userService.selectUserOprecordsById(id);
        if(records!=null){
            return new JsonData(0, records, "查询成功");
        }else {
            return new JsonData(0, records,"查询不成功");
        }
    }

    //查找所有普通用户
    @RequestMapping(value = "selectAllUserByPower",method = RequestMethod.GET)
    @Operation(summary = "查询所在指定权限组的所有用户")
    public JsonData selectAllUserByPower(String power){
        List<User> result=userService.selectAllUserByPower(power);
        if (result != null) {
            return new JsonData(1, result, "查询成功");
        } else {
            return new JsonData(0,result,"没有用户信息");
        }
    }
}
